<template>
  <div class="guess-you-like">
    <!-- 名称 -->
    <div class="title">
      <span class="line"></span>
      <span class="name">猜你喜欢</span>
      <span class="line"></span>
      <div class="clear"></div>
    </div>
    <!-- 商品列表 -->
    <div class="goods-list">
      <div class="scrollBody transition-slow" :style="{'top': `${scrollTop}px`}">
        <!-- 商品（只有价格） -->
        <div class="goods-item" v-for="(item, index) in goodsList" :key="index" @click="onLinkGoodsDetail(item.goodsId)">
          <img v-lazy="item.goodsMainPhotoPath" alt="商品图片">
          <div class="price" v-if="item.storePrice">￥{{item.storePrice.toFixed(2)}}</div>
        </div>
      </div>
    </div>
    <!-- 操作按钮 -->
    <div class="button" v-if="goodsList.length > 3">
      <div class="item" :class="{'disabled': scrollTop == 0}" @click="showBeforePage()">
        <span class="iconfont ico-arrowtop"></span>
      </div>
      <div class="item" :class="{'disabled': scrollTop <= totalHeight}" @click="showNextPage()">
        <span class="iconfont ico-arrowdown"></span>
      </div>
    </div>
  </div>
</template>

<script>
import goodsItem from '../../search/components/GoodsItem'
import { apiGuessYouLike } from '../../../common/api'

export default {
  props: {
    id: {
      type: [Number, String]
    }
  },
  components: {
    goodsItem
  },
  data() {
    return {
      scrollTop: 0,
      totalHeight: -480,
      height: 480,
      goodsList: [],
      params: {
        goodsId: this.id,
        currentPage: 1,
        showCount: 15
      }
    }
  },
  created() {
    apiGuessYouLike(this.params).then(v => {
      this.goodsList = v.goodsLikeList
      this.totalHeight = (v.goodsLikeList.length - 3) * -160
    })
  },
  methods: {
    /**
     * 展示下一页
     */
    showNextPage() {
      // 如果没有滑到底，则继续滑
      if (this.scrollTop > this.totalHeight) {
        this.scrollTop -= this.height
      }
    },

    /**
     * 展示上一页
     */
    showBeforePage() {
      // 如果没有滑到底，则继续滑
      if (this.scrollTop) {
        this.scrollTop += this.height
      }
    },
    /**
     * 根据商品ID跳转商品详情
     */
    onLinkGoodsDetail(goodsId) {
      window.open(`../detail/${goodsId}`)
    }
  }
}
</script>

<style lang="scss" scoped>
@import url('../../../scss/_iconfont.scss');

.guess-you-like {
  .title {
    color: #9a9a9a;
    font-size: 12px;
    text-align: center;

    .line {
      border-bottom: 1px dashed #cccccc;
      display: inline-block;
      float: left;
      width: 34px;
      margin-top: 6px;
      text-decoration: none;
    }

    .name {
      float: left;
      padding: 0px 8px;
    }
  }
  .goods-list {
    margin-top: 10px;
    height: 480px;
    overflow: hidden;
    position: relative;

    .scrollBody {
      position: absolute;

      .goods-item {
        cursor: pointer;
        width: 140px;
        height: 140px;
        position: relative;
        margin-top: 20px;

        .price {
          background-color: rgba(255, 255, 255, 0.8);
          position: absolute;
          width: 100%;
          height: 20px;
          line-height: 20px;
          bottom: 0px;
          text-align: center;
        }

        &:first-child {
          margin-top: 0px;
        }
      }
    }
  }
  .button {
    margin-top: 30px;
    .item {
      cursor: pointer;
      width: 50%;
      float: left;
      text-align: center;

      &.disabled {
        cursor: default;
        color: #a7a7a9;
      }
    }
  }
  .clear {
    clear: both;
  }
}
</style>